
----------------------------------------------------------------------
-- lua面向对象扩展
----------------------------------------------------------------------
-- 
function class(classname, super)
	local superType = type(super)
	local cls
	if superType ~= 'function' and superType ~= 'table' then
		superType = nil
		super = nil
	end

	if superType == 'function' or (super and super.__ctype == 1) then
		-- inherited from native C++ Object
		cls = {}
		if superType == 'table' then
			-- copy fields from super
			for k,v in pairs(super) do cls[k] = v end
			cls.__create = super.__create
			cls.super = super
		else
			cls.__create = super
			cls.ctor = function() end
		end
		cls.__cname = classname
		cls.__ctype = 1

		function cls.new(...)
			local instance = cls.__create(...)
			-- copy fields from class to  native object
			for k, v in pairs(cls) do instance[k] = v end
			instance.class = cls
			instance:ctor(...)
			return instance
		end
        cls.New = cls.new
	else
		-- inherited from Lua Object
		if super then
			cls = {}
			setmetatable(cls, {__index = super})
			cls.super = super
		else
			cls = {ctor = function() end}
		end
		cls.__cname = classname
		cls.__ctype = 2 --lua
		cls.__index = cls
        function cls.ClassName()
        	return cls.__cname
        end
		function cls.new(...)
			local obj = setmetatable({}, cls)
			obj.class = cls			
            if g and type(g)=="table" then 
                g.AddSingleObj(obj)   
            end
            obj:ctor(...)
			return obj
		end
        cls.New = cls.new
	end
	return cls
end

--对象是不是指定的类
function isclass(o, cname)
    if (not o) or (not cname) then
    	return false 
    end
    if (o.__cname ~= cname) and o.super then
        return isclass(o.super, cname)    
    end
    return true
end

--用于需要热更新的类
function import(module_name)    
    local class_module = require(module_name)
    if class_module.__cname then --有名字的类才能更新
        class_module.__cmodule_name = module_name
    end
    return class_module
end


function new(path_name, ...)
	local Object = import(path_name)
	if Object then 
		return Object.new(...)
	end
	print("new " .. path_name .. " error")
	return nil
end